package com.bds.wm.action;

import com.alibaba.fastjson.JSONObject;
import com.bds.wm.model.common.Result;
import com.bds.wm.model.procmodel.*;
import com.bds.wm.service.BrandViewService;
import com.bds.wm.utils.CheckParams;
import com.bds.wm.utils.Const;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * Created by guoyu on 2018/6/1.
 */
@Controller
public class BrandViewAction extends BaseAction {
    private static Logger LOGGER = Logger.getLogger(BrandViewAction.class);

    @Resource
    private BrandViewService brandViewService;

    @RequestMapping(value = "/service/data/get_brand_overview_result", method = RequestMethod.GET)
    @ResponseBody
    public Result getBrandOverviewResult(HttpServletRequest httpRequest, HttpServletResponse httpResponse,
                                         String start_month, String end_month, String brand_name, String subcat_name) {
        Result result = new Result();

        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        try {
            brand_name = URLDecoder.decode(brand_name, "utf-8");
            subcat_name = URLDecoder.decode(subcat_name, "utf-8");
        } catch (UnsupportedEncodingException e) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        if (startMonth == null || endMonth == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        BrandOverviewResult brandOverviewResult = brandViewService
                .getBrandOverviewResult(startMonth, endMonth, brand_name, subcat_name);
        result.setResult(Const.SUC, brandOverviewResult);

        return result;
    }
//
//    @RequestMapping(value = "/service/data/get_brand_overview_result_rate", method = RequestMethod.GET)
//    @ResponseBody
//    public Result getBrandOverviewResultRate(HttpServletRequest httpRequest, HttpServletResponse httpResponse,
//                                             String start_month, String end_month, String brand_name, String subcat_name) {
//        Result result = new Result();
//
//        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
//        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
//        try {
//            brand_name = URLDecoder.decode(brand_name, "utf-8");
//            subcat_name = URLDecoder.decode(subcat_name, "utf-8");
//        } catch (UnsupportedEncodingException e) {
//            LOGGER.info("参数异常");
//            result.setResult(Const.FAIL, "参数异常");
//            return result;
//        }
//        if (startMonth == null || endMonth == null) {
//            LOGGER.info("参数异常");
//            result.setResult(Const.FAIL, "参数异常");
//            return result;
//        }
//
//        BrandOverviewResultRate brandOverviewResultRate = brandViewService
//                .getBrandOverviewResultRate(startMonth, endMonth, brand_name, subcat_name);
//        result.setResult(Const.SUC, brandOverviewResultRate);
//
//        return result;
//    }

    @RequestMapping(value = "/service/data/get_brand_overview_result_by_city", method = RequestMethod.GET)
    @ResponseBody
    public Result getBrandOverviewResultByCity(HttpServletRequest httpRequest, HttpServletResponse httpResponse,
                                               String start_month, String end_month, String brand_name, String subcat_name) {
        Result result = new Result();

        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        String brandName = "", subCatName = "";
        try {
            brandName = URLDecoder.decode(brand_name, "utf-8");
            subCatName = URLDecoder.decode(subcat_name, "utf-8");
        } catch (UnsupportedEncodingException e) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        if (startMonth == null || endMonth == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        JSONObject jsonObject = brandViewService
                .getBrandOverviewResultByCity(startMonth, endMonth, brandName);
        result.setResult(Const.SUC, jsonObject);

        return result;
    }

    @RequestMapping(value = "/service/data/get_brand_map_overview_result", method = RequestMethod.GET)
    @ResponseBody
    public Result getBrandMapOverviewResult(HttpServletRequest httpRequest, HttpServletResponse httpResponse,
                                            String start_month, String end_month, String brand_name, String subcat_name,
                                            String max_lng, String min_lng, String max_lat, String min_lat) {
        Result result = new Result();

        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        Double maxLng = CheckParams.checkIfDoubleAndReturn(max_lng);
        Double minLng = CheckParams.checkIfDoubleAndReturn(min_lng);
        Double maxLat = CheckParams.checkIfDoubleAndReturn(max_lat);
        Double minLat = CheckParams.checkIfDoubleAndReturn(min_lat);
        String brandName = "", subCatName = "";
        try {
            brandName = URLDecoder.decode(brand_name, "utf-8");
            subCatName = URLDecoder.decode(subcat_name, "utf-8");
        } catch (UnsupportedEncodingException e) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        if (startMonth == null || endMonth == null || maxLng == null || minLng == null || maxLat == null || minLat == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }

        List<BrandMapOverviewResult> brandMapOverviewResultList = brandViewService
                .getBrandMapOverviewResult(startMonth, endMonth, brandName, subCatName, maxLng, minLng, maxLat, minLat);
        result.setResult(Const.SUC, brandMapOverviewResultList);

        return result;
    }

    @RequestMapping(value = "/service/data/get_brand_map_overview_result_by_capacity", method = RequestMethod.GET)
    @ResponseBody
    public Result getBrandMapOverviewResultByCapacity(HttpServletRequest httpRequest, HttpServletResponse httpResponse,
                                                      String start_month, String end_month,
                                                      String brand_name, String subcat_name,
                                                      String lng, String lat) {
        Result result = new Result();

        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        Float realLng = CheckParams.checkIfFloatAndReturn(lng);
        Float realLat = CheckParams.checkIfFloatAndReturn(lat);
        String brandName = "", subCatName = "";
        try {
            brandName = URLDecoder.decode(brand_name, "utf-8");
            subCatName = URLDecoder.decode(subcat_name, "utf-8");
        } catch (UnsupportedEncodingException e) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        if (startMonth == null || endMonth == null || realLng == null || realLat == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }

        List<BrandMapOverviewResultByCapacity> list = brandViewService
                .getBrandMapOverviewResultByCapacity(startMonth, endMonth, brandName, subCatName, realLng, realLat);
        result.setResult(Const.SUC, list);

        return result;
    }

    @RequestMapping(value = "/service/data/get_brand_overview_result_by_product", method = RequestMethod.GET)
    @ResponseBody
    public Result getBrandOverviewResultByProduct(HttpServletRequest httpRequest, HttpServletResponse httpResponse,
                                                  String start_month, String end_month, String brand_name,String category) {
        Result result = new Result();

        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        String brandName = "";
        try {
            brandName = URLDecoder.decode(brand_name, "utf-8");
        } catch (UnsupportedEncodingException e) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        if (startMonth == null || endMonth == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }

        JSONObject jsonObject = brandViewService.getBrandOverviewResultByProduct(startMonth, endMonth, brandName,category);
        result.setResult(Const.SUC, jsonObject);

        return result;
    }

    /**
     * 店铺汇总表格2
     * @param start_month
     * @param end_month
     * @return
     */
    @RequestMapping(value = "/service/data/getBrandOverviewResultbycitystore", method = RequestMethod.GET)
    @ResponseBody
    public Result getBrandOverviewResultbycitystore(String start_month,
                                                    String end_month,
                                                    String brandName,
                                                    String subcat_name,
                                                    String cityTier,
                                                    String cityname){
        Result result = new Result();

        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        try {
            brandName = URLDecoder.decode(brandName, "utf-8");
            subcat_name = URLDecoder.decode(subcat_name, "utf-8");
        } catch (UnsupportedEncodingException e) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        if (startMonth == null || endMonth == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        List<BrandOverviewResultByCityStore> brandOverviewResult =
                brandViewService.getBrandOverviewResultbycitystore(start_month,end_month,brandName,
                 subcat_name,cityTier, cityname);
        result.setResult(Const.SUC, brandOverviewResult);

        return result;
    }
    /**
     * 顾客反馈
     * @param start_month
     * @param end_month
     * @return
     */
    @RequestMapping(value = "/service/data/getBrandStoreCommentRatio", method = RequestMethod.GET)
    @ResponseBody
    public Result getBrandStoreCommentRatio(String start_month,
                                                    String end_month,
                                                    String brandName,
                                                    String city){
        Result result = new Result();

        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        try {
            brandName = URLDecoder.decode(brandName, "utf-8");
            city = URLDecoder.decode(city, "utf-8");
        } catch (UnsupportedEncodingException e) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        if (startMonth == null || endMonth == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        JSONObject brandOverviewResult =
                brandViewService.getBrandStoreCommentRatio(start_month,end_month,brandName,
                        city);

        if(brandOverviewResult==null)
            result.setCode(Const.FAIL);
        else
            result.setResult(Const.SUC, brandOverviewResult);

        return result;
    }

    /**
     * 获取城市名、店铺名
     */
    @RequestMapping(value = "/service/data/getBrandCityStoreName", method = RequestMethod.GET)
    @ResponseBody
    public Result getBrandCityStoreName(String end_month, String brand_name) {
        Result result = new Result();

        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        if (end_month == null || brand_name == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        List<BrandCityStoreName> list = brandViewService.getBrandCityStoreName(endMonth, brand_name);
        result.setResult(Const.SUC, list);
        return result;
    }

    /**
     * 评论汇总  月满意度趋势图
     * @param end_month
     * @return
     */
    @RequestMapping(value = "/service/data/getBrandMonthlyRatingTrend", method = RequestMethod.GET)
    @ResponseBody
    public Result getBrandMonthlyRatingTrend(String end_month,
                                             String brandName,
                                             String city){
        Result result = new Result();

        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        try {
            brandName = URLDecoder.decode(brandName, "utf-8");
            city = URLDecoder.decode(city, "utf-8");
        } catch (UnsupportedEncodingException e) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        if (endMonth == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        List<BrandMonthlyRatingTrend> brandMonthlyRatingTrend =
                brandViewService.getBrandMonthlyRatingTrend(end_month,brandName,city);
        result.setResult(Const.SUC, brandMonthlyRatingTrend);

        return result;
    }

    /**
     * 评论汇总  月满意度表格
     * @param end_month
     * @return
     */
    @RequestMapping(value = "/service/data/getBrandStoreRating", method = RequestMethod.GET)
    @ResponseBody
    public Result getBrandStoreRating(String end_month,String start_month,
                                      String brandName,String sortOrder,
                                      String city){
        Result result = new Result();

        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        try {
            brandName = URLDecoder.decode(brandName, "utf-8");
            city = URLDecoder.decode(city, "utf-8");
        } catch (UnsupportedEncodingException e) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        if (endMonth == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        List<BrandStoreRating> brandMonthlyRatingTrend =
                brandViewService.getBrandStoreRating(start_month, end_month,
                        brandName,  city,  sortOrder);
        result.setResult(Const.SUC, brandMonthlyRatingTrend);

        return result;
    }

    /**
     * 评论汇总  月满意度趋势图(模态框)
     * @param end_month
     * @return
     */
    @RequestMapping(value = "/service/data/getBrandStoreRatingTrend", method = RequestMethod.GET)
    @ResponseBody
    public Result getBrandStoreRatingTrend(String end_month,String brandName,String storeName,
                                      String city){
        Result result = new Result();

        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        try {
            brandName = URLDecoder.decode(brandName, "utf-8");
            city = URLDecoder.decode(city, "utf-8");
        } catch (UnsupportedEncodingException e) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        if (endMonth == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        List<BrandStoreRatingTrend> brandMonthlyRatingTrend=brandViewService.getBrandStoreRatingTrend(end_month,
                 brandName,  city,  storeName);
        result.setResult(Const.SUC, brandMonthlyRatingTrend);

        return result;
    }

    /**
     * 品牌分析的词云图
     * @param end_month
     * @return
     */
    @RequestMapping(value = "/service/data/getBrandKeyWord", method = RequestMethod.GET)
    @ResponseBody
    public Result getBrandKeyWord(String end_month,String brandName,String start_month,
                                           String city){
        Result result = new Result();

        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        try {
            brandName = URLDecoder.decode(brandName, "utf-8");
            city = URLDecoder.decode(city, "utf-8");
        } catch (UnsupportedEncodingException e) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        if (endMonth == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        List<BrandKeyWord> brandMonthlyRatingTrend=brandViewService.getBrandKeyWord(end_month,
                 brandName,  city,  start_month);
            result.setResult(Const.SUC, brandMonthlyRatingTrend);

        return result;
    }

    /**
     * 老店客单
     */
    @RequestMapping(value = "/service/data/getBrandOverviewResultofOldStore", method = RequestMethod.GET)
    @ResponseBody
    public Result getBrandOverviewResultofOldStore(String end_month,String brand_name,String start_month,
                                  String subcate){
        Result result = new Result();

        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        if (endMonth == null||startMonth==null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        BrandOverviewResultofOldStore brandMonthlyRatingTrend=brandViewService.getBrandOverviewResultofOldStore(startMonth,endMonth,brand_name);
        result.setResult(Const.SUC, brandMonthlyRatingTrend);

        return result;
    }

    @RequestMapping(value = "/service/data/getBrandNews", method = RequestMethod.GET)
    @ResponseBody
    public Result getBrandNews(String end_month,String brand_name,String start_month){
        Result result = new Result();

        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        if (endMonth == null||startMonth==null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        List<BrandNews> brandNews = brandViewService.getBrandNews(startMonth,endMonth,brand_name);
        result.setResult(Const.SUC, brandNews);

        return result;
    }
}
